﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace smallDeimos.Game
{
    public partial class Agreements : System.Web.UI.Page
    {
        int UserId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
                Button1.Text = HttpUtility.HtmlEncode("Send invitation($" + StoredProcedures.TACost(UserId) + ")");
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }

        }
        protected string StatusDescription(object sender)
        {
            if (sender != null)
            {
                if (sender.ToString().Equals("True") == true)
                    return "Operational";
                else if (sender.ToString().Equals("False") == true)
                    return "Pending";
            }
            return string.Empty;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                bool visible = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "Status"));
                Control c = e.Row.FindControl("Button2");
                if (c != null)
                {
                    Button btn = c as Button;
                    btn.Attributes.Add("From", DataBinder.Eval(e.Row.DataItem, "From").ToString());
                    btn.Attributes.Add("To", DataBinder.Eval(e.Row.DataItem, "To").ToString());
                    btn.Visible = !visible;//since 0 means pending
                }

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (StoredProcedures.SendAgreement(UserId, UserDefinedFunctions.GetPlayerID(TextBox1.Text).Value, 0) == false)
            {
                Label2.Text = "Agreement offer cannot be send.";
            }
            else
            {
                Label2.Text = "Agreement offer sent.";
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (StoredProcedures.AcceptAgreement(UserDefinedFunctions.GetPlayerID(btn.Attributes["From"]).Value, UserDefinedFunctions.GetPlayerID(btn.Attributes["To"]).Value, 0) == true)
            {
                Label2.Text = "Agreement offer accepted.";
            }
            else
            {
                Label2.Text = "Agreement offer cannot be accpeted.";
            }
        }
    }
}
